<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column label="头像" width="150" align="center">
        <template slot-scope="scope">
          <el-avatar
            shape="square"
            size="small"
            :src="scope.row.avatar"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="昵称" width="180">
        <template slot-scope="scope">{{ scope.row.nickname }}</template>
      </el-table-column>
      <el-table-column label="评论文章" width="180">
        <template slot-scope="scope">{{ scope.row.blog.title }}</template>
      </el-table-column>
      <el-table-column label="评论内容">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>
      <el-table-column label="创建日期" width="220">
        <template slot-scope="scope">{{ scope.row.createDate }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteCommenthandle(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 30px"
      background
      layout="prev, pager, next, total, ->, sizes, jumper"
      :total="count"
      :page-size="eachPage"
      :page-sizes="[3, 10, 20, 30]"
      :current-page.sync="pageCurrentPage"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      @prev-click="prevClickHandle"
      @next-click="nextClickHandle"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getComment, deleteComment } from "@/api/comment";
import { formatDate } from "@/utils/tools.js";
import url from "@/urlConfig.js";
export default {
  data() {
    return {
      tableData: [],
      eachPage: 3, // 每一页显示的条数
      currenPage: 1, // 当前页码，默认第一页
      totalPage: 0, // 总页数
      count: 0, // 数据总数
      pageCurrentPage: 1, // 分页当前页码
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      getComment(this.currenPage, this.eachPage).then(({ data }) => {
        this.tableData = data ? data.rows : [];
        this.count = data.total;
        for (const item of this.tableData) {
          item.createDate = formatDate(item.createDate);
          item.avatar = url.server_URL + item.avatar;
        }
        this.totalPage = Math.ceil(this.count / this.eachPage);
        if (this.currenPage > this.totalPage && this.count > 0) {
          this.currenPage = this.totalPage;
          this.fetchData();
        }
      });
    },
    // 删除留言
    deleteCommenthandle({ id }) {
      deleteComment();
      this.$confirm(
        "是否删除该条评论?", "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteComment(id).then(() => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页相关设置
    sizeChangeHandle(pagerNubmer) {
      this.eachPage = parseInt(pagerNubmer);
      this.currenPage = 1;
      this.pageCurrentPage = 1;
      this.fetchData();
    },
    currentChangeHandle(pagerNubmer) {
      this.currenPage = parseInt(pagerNubmer);
      this.fetchData();
    },
    prevClickHandle() {
      this.currenPage -= 1;
    },
    nextClickHandle() {
      this.currenPage += 1;
    },
  },
};
</script>

<style>
</style>
